package org.shock.ss.sms.action.helpdoc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import org.shock.ss.sms.action.BaseAction;
import org.shock.ss.sms.bean.Page;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.bean.helpdoc.Helpdoc;
import org.shock.ss.sms.bean.system.Module;
import org.shock.ss.sms.bean.system.UserInfor;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.commons.utils.ParamsUtil;
import org.shock.ss.sms.commons.utils.ReflexUtils;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.helpdoc.impl.HelpdocDaoImpl;
import org.shock.ss.sms.service.helpdoc.HelpdocService;
import org.shock.ss.sms.service.helpdoc.impl.HelpdocServiceImpl;

public class HelpdocAction extends BaseAction {
	/** 获得service */
	private HelpdocService helpS = new HelpdocServiceImpl((HelpdocDaoImpl) DaoFactory.getDao("HelpdocDao"));
	private List list = new ArrayList();
	private Map map = new HashMap();
	private Helpdoc helpBean = new Helpdoc();// 帮助文档的bean对象
	private List urlList;

	/**
	 * 进入帮助文档添加页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "帮助文档管理")
	public String execute() throws Exception {
		// 如果未登录，自动生成一个guest账户
		if (null == getUserInfor()) {
			UserInfor userinfor = new UserInfor();
			userinfor.setYgzh("guest");
			userinfor.setYgxm("游客");
			userinfor.setRoleids("-1");
			userinfor.setIsadmin(0);
			getSession().setAttribute("userinfor", userinfor);
		}
		return SUCCESS;
	}

	/**
	 * 进入帮助文档添加页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightValue = "addhelpdoc", rightNote = "添加帮助文档")
	public String addHelpdoc() throws Exception {
		if (action.equals("before")) {// 显示添加页面
			String sql = "select id,name from " + Module.getTable() + " order by name";
			urlList = DbTools.getList(sql);
			return "before";
		}

		// 获得session
		UserInfor user = getUserInfor();
		helpBean.setCzr(user.getYgxm());
		helpBean.setCzsj(Util.getXxTime());
		if (null == helpBean.getNr() || "".equals(helpBean.getNr())) {
			helpBean.setNr(" ");
		}
		helpS.doAddHelpdoc(helpBean);
		message = "添加成功！";
		jmpUrl = "helpdocList.do";
		return SUCCESS;
	}

	/**
	 * 进入修改帮助文档页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightValue = "helpdocUpdate", rightNote = "修改帮助文档")
	public String helpdocUpdate() throws Exception {
		if (action.equals("before")) {// 显示添加页面
			long id = helpBean.getId();
			id = (id - 0) > 0 ? id : -1;
			String sql = "select h.*,m.name modname from ckgl_helpdoc h," + Module.getTable()
					+ " m  where h.modid=m.id and h.id=" + id;
			map = helpS.view(sql);
			// sql="select id,name from module order by name";
			// urlList=DbTools.getList(sql);
		} else {
			helpS.update(helpBean);
			message = "修改成功！";
			jmpUrl = "helpdocList.do";
		}
		return SUCCESS;
	}

	public String saveHelpdoc() throws Exception {
		BeanUtils.populate(helpBean, map);
		Set keyset=map.keySet();
		for (Object key : keyset) {
			System.out.println(key+":"+((String[])map.get(key))[0]);
		}
		System.out.println(ReflexUtils.rwritertoString(helpBean));
		if (helpBean.getId() == 0) {
			addHelpdoc();
		} else {
			helpdocUpdate();
		}
		return SUCCESS;
	}

	/**
	 * 删除帮助文档
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightValue = "helpdocDel", rightNote = "删除帮助文档")
	public String helpdocDel() throws Exception {
		helpS.del(helpBean);
		message = "删除成功";
		jmpUrl = "helpdocList.do";
		return SUCCESS;
	}

	/**
	 * 查看帮助文档
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightValue = "public", rightNote = "查看帮助文档")
	public String helpdocView() throws Exception {
		long id = helpBean.getId();
		id = (id - 0) > 0 ? id : -1;
		String sql = "select h.*,m.name from ckgl_helpdoc h," + Module.getTable() + " m where h.modid=m.id and h.id="
				+ id;
		if (null != helpBean.getSsurl() && !"".equals(helpBean.getSsurl())) {
			sql = "select h.*,m.name from ckgl_helpdoc h," + Module.getTable() + " m where h.modid=m.id and h.ssurl='"
					+ helpBean.getSsurl().trim() + "'";
		}
		System.out.println(sql);
		map = helpS.view(sql);
		System.out.println(map);
		return SUCCESS;
	}

	/**
	 * 查看帮助文档列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightValue = "public", rightNote = "帮助文档列表")
	public String helpdocList() throws Exception {
		if (null != helpBean) {
			if (0 != helpBean.getModid()) {
				sqladd.append(" and h.modid ='" + helpBean.getModid() + "'");
			}
		}
		Pager pager = new Pager();
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		String sqlcount = "select count(1) from ckgl_helpdoc h," + Module.getTable() + " m where h.modid=m.id "
				+ sqladd.toString();
		String sql = "select h.*,m.name from ckgl_helpdoc h," + Module.getTable() + " m where h.modid=m.id "
				+ sqladd.toString();
		pager = helpS.list("desc", "h.id", pageno, rownum, sqlcount, sql);
		list = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		sql = "select id,name from " + Module.getTable() + " order by name";
		urlList = DbTools.getList(sql);
		return SUCCESS;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Helpdoc getHelpBean() {
		return helpBean;
	}

	public void setHelpBean(Helpdoc helpBean) {
		this.helpBean = helpBean;
	}

	public List getUrlList() {
		return urlList;
	}

	public void setUrlList(List urlList) {
		this.urlList = urlList;
	}

}
